package com.cz.nongyetongv5.activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.City;
import com.cz.nongyetongv5.bean.CityBean;
import com.cz.nongyetongv5.bean.JsonUtil;
import com.cz.nongyetongv5.bean.PatrolDetailBean;
import com.cz.nongyetongv5.bean.PatrolList;
import com.cz.nongyetongv5.bean.PatrolListBean;
import com.cz.nongyetongv5.bean.PatrolListResponse;
import com.cz.nongyetongv5.bean.PopBean;
import com.cz.nongyetongv5.bean.StayCheckBean;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.fragment.AreaPopPatrolNewFragment;
import com.cz.nongyetongv5.fragment.FragmentAdapter;
import com.google.gson.Gson;
import com.ronri.common.WaitDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class PatrolListNewActivity extends FragmentActivity implements View.OnClickListener {
    private RelativeLayout rl_back;
    private TextView tv_right;

    private List<PopBean> types;
    private TypeAdapter typeAdapter;
    private LinearLayout ll_type;
    private TextView tv_type;
    private LinearLayout ll_type_list;
    private RecyclerView rv_type;
    private LinearLayout ll_alpha;
    private RecyclerView rv_list;

    private LinearLayout ll_bottom;
    private TextView tv_pass;

    private String add_user;
    private String region_code;


    private int page = 1;
    private int size = 10;
    private PatrolListAdapter adapter;

    SmartRefreshLayout refreshLayout;
    private boolean isShowEdit = false;
    private String ids = "";


    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    //ViewPage选项卡页面集合
    private List<Fragment> mFragments;
    private FragmentAdapter mFragmentAdapter;
    //Tab标题集合
    private List<String> mTitles;
    private RelativeLayout ll_area_pop;
    private TextView tv_confirm;
    private LinearLayout ll_area;
    private TextView tv_area;
    private User user;

    private String code;
    private String name = "";

    private CheckBox cb_choose;
    private LinearLayout ll_choose;

    private TextView tv_count;

    private StayCheckBean stayCheckBean;


    private PatrolListResponse patrolListResponse;
    private PatrolListBean patrolListBean;
    private List<PatrolList> patrolLists;

    private String type = "0";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patrol_list_new);
        user = (User) getIntent().getSerializableExtra("user");
        add_user = user.getUsername();
        region_code = user.getKnowRegion();
        code = user.getKnowRegion();
        name = user.getRegion_name();
        types = new ArrayList<>();

        mTitles = new ArrayList<>();
        mFragments = new ArrayList<>();
        types.add(new PopBean("主体对象巡查", false, "1"));
        types.add(new PopBean("其他巡查", false, "2"));
        types.add(new PopBean("全部", true, "0"));
        rl_back = findViewById(R.id.rl_back);
        ll_type_list = findViewById(R.id.ll_type_list);
        ll_alpha = findViewById(R.id.ll_alpha);
        rl_back.setOnClickListener(this);
        tv_right = findViewById(R.id.tv_right);
        tv_right.setText("批量处理");
        tv_right.setOnClickListener(this);
        tv_type = findViewById(R.id.tv_type);
        tv_type.setText("全部");
        rv_type = findViewById(R.id.rv_type);
        ll_bottom = findViewById(R.id.ll_bottom);
        tv_pass = findViewById(R.id.tv_pass);

        tv_pass.setOnClickListener(this);

        typeAdapter = new TypeAdapter();
        rv_type.setLayoutManager(new LinearLayoutManager(PatrolListNewActivity.this));
        rv_type.setAdapter(typeAdapter);
        ll_alpha.setOnClickListener(this);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (PopBean popupBean : types) {
                    popupBean.setChecked(false);
                }
                types.get(position).setChecked(true);
                adapter.notifyDataSetChanged();
                tv_type.setText(types.get(position).getName());
                //                rv_year.setVisibility(View.GONE);
                ll_type_list.setVisibility(View.GONE);
                tv_type.setTextColor(Color.parseColor("#585858"));
                tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
                        0, R.drawable.jt_down_black, 0);
                type = types.get(position).getLacationTag();
                page = 1;
                WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
                getPatrolList();
            }
        });
        ll_type = findViewById(R.id.ll_type);
        ll_type.setOnClickListener(this);
        rv_list = findViewById(R.id.rv_list);
        refreshLayout = findViewById(R.id.refreshLayout);
        patrolLists = new ArrayList<>();
        adapter = new PatrolListAdapter();
        View notDataView = getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) rv_list.getParent(), false);
        adapter.setEmptyView(notDataView);
        rv_list.setLayoutManager(new LinearLayoutManager(PatrolListNewActivity.this));
        rv_list.setAdapter(adapter); //设置分页和下拉刷新
        refreshLayout.setDisableContentWhenRefresh(true);
        refreshLayout.setDisableContentWhenLoading(true);
        refreshLayout.setRefreshHeader(new ClassicsHeader(PatrolListNewActivity.this));
        //设置 Footer
        refreshLayout.setRefreshFooter(new ClassicsFooter(PatrolListNewActivity.this));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
                getPatrolList();
                //                WaitDialog.ShowDialog(getActivity(),"数据加载中...");

            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (patrolListBean != null) {
                    if (page < patrolListBean.getTotal()) {
                        page++;
                        WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
                        getPatrolList();
                    } else {
                        refreshLayout.finishLoadmore();
                        Toast.makeText(PatrolListNewActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    refreshLayout.finishLoadmore();
                    Toast.makeText(PatrolListNewActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
                getPatrolDetail(patrolLists.get(position).getCheck_id());
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.ll_root || view.getId() == R.id.rl_ent) {
                    getPatrolDetail(patrolLists.get(position).getCheck_id());
                } else {
                    if (patrolLists.get(position).isChecked()) {
                        patrolLists.get(position).setChecked(false);
                    } else {
                        patrolLists.get(position).setChecked(true);
                    }
                    adapter.notifyDataSetChanged();
                    int sum = 0;
                    for (PatrolList patrolList : patrolLists) {
                        if (patrolList.isChecked()) {
                            sum++;
                        }
                    }
                    if (sum == patrolLists.size()) {
                        cb_choose.setChecked(true);
                    } else {
                        cb_choose.setChecked(false);
                    }
                    if (stayCheckBean != null) {
                        if (sum == 0) {
                            SpannableString ss = new SpannableString("待处理巡查" + String.valueOf(stayCheckBean.getData().getCheckCount()) + "条");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        } else {
                            SpannableString ss = new SpannableString("待处理巡查" + String.valueOf(stayCheckBean.getData().getCheckCount()) + "条,已选择巡查" + String.valueOf(sum) + "条");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 12 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), 12 + String.valueOf(stayCheckBean.getData().getCheckCount()).length() + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        }
                    } else {
                        if (sum == 0) {
                            tv_count.setText("暂无数据");
                        } else {
                            SpannableString ss = new SpannableString("已选择巡查" + String.valueOf(sum) + "条");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        }
                    }
                }

            }
        });

        ll_area = findViewById(R.id.ll_area);
        ll_area.setOnClickListener(this);
        tv_area = findViewById(R.id.tv_area);
        tv_area.setText(user.getRegion_name());
        ll_area_pop = findViewById(R.id.ll_area_pop);
        mViewPager = findViewById(R.id.viewpager);
        mTabLayout = findViewById(R.id.tab);
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener(this);


        cb_choose = findViewById(R.id.cb_choose);
        ll_choose = findViewById(R.id.ll_choose);
        ll_choose.setOnClickListener(this);


        tv_count = findViewById(R.id.tv_count);
        WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
        getPatrolList();
        getStay();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.ll_choose:
                if (cb_choose.isChecked()) {
                    cb_choose.setChecked(false);
                    setSelect(false);
                } else {
                    cb_choose.setChecked(true);
                    setSelect(true);
                }
                int sum = 0;
                for (PatrolList patrolList : patrolLists) {
                    if (patrolList.isChecked()) {
                        sum++;
                    }
                }
                if (sum == patrolLists.size()) {
                    cb_choose.setChecked(true);
                } else {
                    cb_choose.setChecked(false);
                }
                if (stayCheckBean != null) {
                    if (sum == 0) {
                        SpannableString ss = new SpannableString("待处理巡查" + String.valueOf(stayCheckBean.getData().getCheckCount()) + "条");
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_count.setText(ss);
                    } else {
                        SpannableString ss = new SpannableString("待处理巡查" + String.valueOf(stayCheckBean.getData().getCheckCount()) + "条,已选择巡查" + String.valueOf(sum) + "条");
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 12 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), 12 + String.valueOf(stayCheckBean.getData().getCheckCount()).length() + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_count.setText(ss);
                    }
                } else {
                    if (sum == 0) {
                        tv_count.setText("暂无数据");
                    } else {
                        SpannableString ss = new SpannableString("已选择巡查" + String.valueOf(sum) + "条");
                        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(sum).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_count.setText(ss);
                    }
                }
                break;
            case R.id.ll_alpha:
                if (ll_type_list.getVisibility() == View.VISIBLE) {
                    ll_type_list.setVisibility(View.GONE);
                    tv_type.setTextColor(Color.parseColor("#585858"));
                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                break;
            case R.id.tv_right:
                if (ll_area_pop.getVisibility() == View.VISIBLE) {
                    ll_area_pop.setVisibility(View.GONE);
                    tv_area.setTextColor(Color.parseColor("#585858"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                if (ll_type_list.getVisibility() == View.VISIBLE) {
                    ll_type_list.setVisibility(View.GONE);
                    tv_type.setTextColor(Color.parseColor("#585858"));
                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }

                if (patrolLists != null && patrolLists.size() == 0) {
                    Toast.makeText(PatrolListNewActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
                } else {
                    if (ll_bottom.getVisibility() == View.VISIBLE) {
                        isShowEdit = false;
                        ll_bottom.setVisibility(View.GONE);
                        tv_right.setText("批量处理");
                    } else {
                        isShowEdit = true;
                        ll_bottom.setVisibility(View.VISIBLE);
                        tv_right.setText("取消");
                    }
                    cb_choose.setChecked(false);
                    setSelect(false);
                    adapter.notifyDataSetChanged();
                }
                if (stayCheckBean != null) {
                    SpannableString ss = new SpannableString("待处理巡查" + String.valueOf(stayCheckBean.getData().getCheckCount()) + "条");
                    ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_count.setText(ss);
                } else {
                    tv_count.setText("暂无数据");
                }
                break;
            case R.id.ll_type:
                if (ll_area_pop.getVisibility() == View.VISIBLE) {
                    ll_area_pop.setVisibility(View.GONE);
                    tv_area.setTextColor(Color.parseColor("#585858"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                if (ll_type_list.getVisibility() == View.GONE) {
                    ll_type_list.setVisibility(View.VISIBLE);
                    tv_type.setTextColor(Color.parseColor("#21b481"));
                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_up_green, 0);
                } else {
                    ll_type_list.setVisibility(View.GONE);
                    tv_type.setTextColor(Color.parseColor("#585858"));
                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                break;
            case R.id.tv_pass:
                ids = "";
                List<PatrolList> lists = getSelected();
                if (lists.size() <= 0) {
                    Toast.makeText(PatrolListNewActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < lists.size(); i++) {
                        if (i == 0) {
                            ids = lists.get(i).getCheck_id();
                        } else {
                            ids = ids + "," + lists.get(i).getCheck_id();
                        }
                    }
                    Intent intent = new Intent(PatrolListNewActivity.this, DialogActivity.class);
                    intent.putExtra("add_user", add_user);
                    intent.putExtra("region_code", region_code);
                    intent.putExtra("ids", ids);
                    intent.putExtra("model", "patrol");
                    startActivityForResult(intent, 9003);
                }
                break;
            case R.id.tv_refuse:
                ids = "";
                if (getSelected().size() <= 0) {
                    Toast.makeText(PatrolListNewActivity.this, "没有选择数据", Toast.LENGTH_SHORT).show();
                } else {
                    for (int i = 0; i < getSelected().size(); i++) {
                        if (i == 0) {
                            ids = String.valueOf(getSelected().get(i).getCheck_id());
                        } else {
                            ids = ids + "," + getSelected().get(i).getCheck_id();
                        }
                    }
                    Intent intent2 = new Intent(PatrolListNewActivity.this, DialogActivity.class);
                    intent2.putExtra("add_user", add_user);
                    intent2.putExtra("region_code", region_code);
                    intent2.putExtra("ids", ids);
                    intent2.putExtra("type", "2");
                    intent2.putExtra("model", "ent");
                    startActivityForResult(intent2, 9002);
                }
                break;
            case R.id.ll_area:
                if (ll_type_list.getVisibility() == View.VISIBLE) {
                    ll_type_list.setVisibility(View.GONE);
                    tv_type.setTextColor(Color.parseColor("#585858"));
                    tv_type.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                if (ll_area_pop.getVisibility() == View.GONE) {
                    mTitles.clear();
                    mFragments.clear();
                    mTitles.add(user.getRegion_name());
                    List<City> cities = new ArrayList<>();
                    City city = new City();
                    city.setCode(user.getKnowRegion());
                    city.setName(user.getRegion_name());
                    city.setChecked(true);
                    cities.add(city);
                    mFragments.add(AreaPopPatrolNewFragment.newInstance(cities));
                    tv_area.setTextColor(Color.parseColor("#21b481"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_up_green, 0);

                    //                    if (user.getKnowRegion().length() == 8) {
                    //                        initTablayout();
                    //                    } else {
                    WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
                    getCity(user.getKnowRegion());
                    //                    }
                } else {
                    ll_area_pop.setVisibility(View.GONE);
                    tv_area.setTextColor(Color.parseColor("#585858"));
                    tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                            0, R.drawable.jt_down_black, 0);
                }
                break;

            case R.id.tv_confirm:
                for (int i = mFragments.size() - 1; i >= 0; i--) {
                    if (((AreaPopPatrolNewFragment) mFragments.get(i)).getSelectCity() != null) {
                        tv_area.setText(((AreaPopPatrolNewFragment) mFragments.get(i)).getSelectCity().getName());
                        code = ((AreaPopPatrolNewFragment) mFragments.get(i)).getSelectCity().getCode();
                        name = ((AreaPopPatrolNewFragment) mFragments.get(i)).getSelectCity().getName();
                        tv_area.setTextColor(Color.parseColor("#585858"));
                        tv_area.setCompoundDrawablesWithIntrinsicBounds(0,
                                0, R.drawable.jt_down_black, 0);
                        ll_area_pop.setVisibility(View.GONE);
                        page = 1;
                        WaitDialog.ShowDialog(PatrolListNewActivity.this, "数据加载中...");
                        getPatrolList();
                        return;
                    }
                }
                break;

            default:
                break;
        }
    }

    public void setSelect(boolean isSelect) {
        for (PatrolList patrolList : patrolLists) {
            patrolList.setChecked(isSelect);
        }
        adapter.notifyDataSetChanged();
    }

    public class TypeAdapter extends BaseQuickAdapter<PopBean, BaseViewHolder> {
        public TypeAdapter() {
            super(R.layout.status_pop_item, types);
        }

        @Override
        protected void convert(BaseViewHolder helper, PopBean item) {
            if (item.isChecked()) {
                helper.setTextColor(R.id.tv_name, Color.parseColor("#21b481"));
            } else {
                helper.setTextColor(R.id.tv_name, Color.parseColor("#585858"));
            }
            helper.setText(R.id.tv_name, item.getName());
        }
    }

    void getPatrolList() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "check/suspiciousCheckList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("page", String.valueOf(page))
                .addParams("size", String.valueOf(size))
                .addParams("region_code", code)
                .addParams("type", type)
                .addParams("is_operate", String.valueOf("1"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        closeRefresh();
                        WaitDialog.HideDialog();
                        Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        WaitDialog.HideDialog();
                        closeRefresh();
                        patrolListResponse = JsonUtil.toObject(response, PatrolListResponse.class);
                        if (patrolListResponse.getCode() == 0) {
                            patrolListBean = patrolListResponse.getData();
                            if (page == 1) {
                                patrolLists.clear();
                                patrolLists.addAll(patrolListBean.getRows());
                            } else {
                                patrolLists.addAll(patrolListBean.getRows());
                            }
                            if (patrolLists.size() == 0) {
                                isShowEdit = false;
                                tv_right.setText("批量处理");
                                ll_bottom.setVisibility(View.GONE);
                                adapter.notifyDataSetChanged();
                            } else {
                                int sum = 0;
                                for (PatrolList patrolList : patrolLists) {
                                    if (patrolList.isChecked()) {
                                        sum++;
                                    }
                                }
                                if (sum == patrolLists.size()) {
                                    cb_choose.setChecked(true);
                                } else {
                                    cb_choose.setChecked(false);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        } else {
                            Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    public class PatrolListAdapter extends BaseQuickAdapter<PatrolList, BaseViewHolder> {
        public PatrolListAdapter() {
            super(R.layout.patrol_list_item, patrolLists);
        }

        @Override
        protected void convert(BaseViewHolder helper, PatrolList item) {
            if (item.getType() != null && item.getType().equals("1")) {
                helper.setText(R.id.tv_patrol_addr, "巡查对象:");
                helper.setText(R.id.tv_record_hit, "巡查内容:");
                if (item.getCheck_address() != null) {
                    helper.setText(R.id.tv_product_name, item.getEnt_name());
                } else {
                    helper.setText(R.id.tv_product_name, "--");
                }

                if (item.getContent() != null) {
                    helper.setText(R.id.tv_record, item.getContent());
                } else {
                    helper.setText(R.id.tv_record, "--");
                }
            } else {
                helper.setText(R.id.tv_patrol_addr, "巡查地点:");
                helper.setText(R.id.tv_record_hit, "巡查记录:");
                if (item.getCheck_address() != null) {
                    helper.setText(R.id.tv_product_name, item.getCheck_address());
                } else {
                    helper.setText(R.id.tv_product_name, "--");
                }

                if (item.getCheck_record() != null) {
                    helper.setText(R.id.tv_record, item.getCheck_record());
                } else {
                    helper.setText(R.id.tv_record, "--");
                }
            }

            if (item.getRealname() != null) {
                helper.setText(R.id.tv_admin, item.getRealname() + "(" + item.getRegion_name() + ")");
            }
            if (item.getCheck_time() != null) {
                helper.setText(R.id.tv_date, item.getCheck_time());
            }


            if (isShowEdit) {
                helper.setVisible(R.id.cb_choose, true);
            } else {
                helper.setVisible(R.id.cb_choose, false);
            }
            helper.addOnClickListener(R.id.cb_choose);
            helper.setChecked(R.id.cb_choose, item.isChecked());
            Glide.with(PatrolListNewActivity.this)
                    .load(item.getAttach_url()).placeholder(R.drawable.loading).error(R.mipmap.ic_launcher)
                    .into((ImageView) helper.getView(R.id.iv_iv));

        }
    }

    public void closeRefresh() {
        if (refreshLayout != null) {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh();
            } else if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadmore();
            }
        }
    }


    void getPatrolDetail(String id) {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "check/checkCheckDetail.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("check_id", id)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        WaitDialog.HideDialog();
                        Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        WaitDialog.HideDialog();
                        Gson gson = new Gson();
                        PatrolDetailBean patrolDetailBean = gson.fromJson(response, PatrolDetailBean.class);
                        //                        EntDetailBean entDetailBean = JsonUtil.toObject(response, EntDetailBean.class);
                        if (patrolDetailBean.getCode() == 0) {
                            if (patrolDetailBean.getData() != null) {
                                Intent intent = new Intent(PatrolListNewActivity.this, PatrolDetailActivity.class);
                                intent.putExtra("add_user", add_user);
                                intent.putExtra("region_code", region_code);
                                intent.putExtra("patrolBean", patrolDetailBean.getData());
                                startActivityForResult(intent, 9003);
                            } else {
                                Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }

                        } else {
                            Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    /**
     * 获取选中的item
     *
     * @return
     */
    public List<PatrolList> getSelected() {
        List<PatrolList> selectedItems = new ArrayList<>();
        for (PatrolList patrolList : patrolLists) {
            if (patrolList.isChecked()) {
                selectedItems.add(patrolList);
            }
        }
        return selectedItems;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 9003:
                    page = 1;
                    //                    editType = 1;
                    //                    isShowEdit = false;
                    //                    tv_right.setText("审核");
                    //                    ll_bottom.setVisibility(View.GONE);
                    //                    cb_choose.setChecked(false);
                    WaitDialog.ShowDialog(PatrolListNewActivity.this,"数据加载中....");
                    getPatrolList();
                    getStay();
                    break;
                default:
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 获取区域
     */
    void getCity(String code) {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "system/regionList.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        WaitDialog.HideDialog();
                        //                                                Toast.makeText(AssistEntUpdateActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);\
                        WaitDialog.HideDialog();
                        CityBean cityBeann = JsonUtil.toObject(response, CityBean.class);
                        if (cityBeann.getCode() == 0) {
                            if (cityBeann.getData().size() > 0) {
                                mTitles.add("请选择");
                                mFragments.add(AreaPopPatrolNewFragment.newInstance(cityBeann.getData()));
                                initTablayout();
                            } else {
                                initTablayout();
                            }
                        } else {
                            Toast.makeText(PatrolListNewActivity.this, "获取数据失败，稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void initTablayout() {
        ll_area_pop.setVisibility(View.VISIBLE);
        mFragmentAdapter = null;
        mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setAdapter(mFragmentAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来
        mViewPager.setCurrentItem(mFragments.size() - 1);
        mTabLayout.getTabAt(mFragments.size() - 1).select();
    }

    public void setUpdate(String name, String code) {
        //        ToastUtils.showToast(AreaActivity.this, code + name);
        //        ToastUtils.showToast(AreaActivity.this, mViewPager.getCurrentItem() + name + mTitles.size());
        List<String> tmp = new ArrayList<>();
        List<Fragment> tmpFragments = new ArrayList<>();
        for (int i = 0; i <= mViewPager.getCurrentItem(); i++) {
            tmp.add(mTitles.get(i));
            tmpFragments.add(mFragments.get(i));
        }
        tmp.add(tmp.size() - 1, name);
        tmp.remove(tmp.size() - 1);
        tmp.add("请选择");
        mTitles.clear();
        mTitles.addAll(tmp);
        mFragments.clear();
        mFragments.addAll(tmpFragments);
        //        resultCode = code;
        getCity(code);
    }


    /**
     * 获取待办
     */
    void getStay() {
        OkHttpUtils
                .get()
                .url(InitApplication.newUrl + "system/getCheckMatter.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("region_code", user.getKnowRegion())
                .addParams("realname", user.getRealname())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        tv_count.setText("暂无数据");
                        //                        Toast.makeText(PatrolListNewActivity.this, "请求失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        //                        System.out.println(response);
                        stayCheckBean = JsonUtil.toObject(response, StayCheckBean.class);
                        if (stayCheckBean.getCode().equals("0")) {
                            SpannableString ss = new SpannableString("待处理巡查" + String.valueOf(stayCheckBean.getData().getCheckCount()) + "条");
                            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#ff3d5d")), 5, 5 + String.valueOf(stayCheckBean.getData().getCheckCount()).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_count.setText(ss);
                        } else {
                            tv_count.setText("暂无数据");
                        }
                    }
                });
    }

}
